package old.gameAppLayer;

//import com.jme.app.BaseGame;
import java.awt.Cursor;
import java.io.File;

import old.resourceCache.IResourceFile;
import old.resourceCache.ResCache;
import old.resourceCache.ZipArchive;



import com.jme.renderer.Renderer;
import com.jme.system.DisplaySystem;
import com.jme.system.JmeException;

import eventManager.EventManager;


public class GameAppLayer
{
	public static final int MEGABYTE = 1024 * 1024;
	public DisplaySystem display;
	public Renderer renderer;
	public GameOptions pOptions;
	public LuaStateManager luaStateManager;
	public EventManager eventManager;
	public ResCache resCache;
	
	public boolean quitRequested = false;
	public boolean quitting = false;
	public int hadModalDialog = 0;
	
	
	GCERROR error;
	GCSOLUTION solution;
	
	
	public boolean initInstance()
	{
		//Check for existing instances of the game
		//TBD
		
		
		//Load language DLL
		//TBD
		
		
		//Check machine resources
		boolean resourceCheck = false;
		int counter = 0;
		while (!resourceCheck)
		{
			counter++;
			try {
				int physicalRAM = 512 * MEGABYTE;
				int virtualRAM = 1024 * MEGABYTE;
				checkMemory(physicalRAM, virtualRAM);
				
				int diskSpace = 10 * MEGABYTE;
				checkHardDisk(diskSpace);
				
				int minCPUSpeed = 1300;  //GHz
				int thisCPU = getCPUSpeed();
				if (thisCPU < minCPUSpeed)
					throw new GameCodeError(error.GCERR_INIT_CPU_TOO_SLOW);
			}
			catch (GameCodeError err) {
				if ((err.handle()==solution.ERROR_RETRY) && (counter < 5))
					continue;
				else return false;
			}
						
			resourceCheck = true;
		}
		
		pOptions = new GameOptions(/*TBD*/);
		//ParseCommandLine(lpCmdLine);  ???
		
		
		//Initialize resource cache
		File gameRes = new File("data\\GameRes.zip");
		resCache = new ResCache(30, new ZipArchive(gameRes));
		
		
		//Startup the Lua state manager and run initial script
		luaStateManager = new LuaStateManager();
		//TBD^^^!!!!!!
		
		eventManager = new EventManager();
		
		registerBaseGameEvents(); //register all base event types
		
		//Since LUA and Event Mgrs are started, run intialization file
		//TBD, run LUA init file for scripts??
		
		
		
		//Load user settings (Note: online code has this after display window initialization)
		//Find save game, profile directory(s)
		
		
		//Set display window up
		//NOTE:  Currently has hardcoded parameters, must make them variable
		//If no previous instances, create display system
		try {
		      //we will create an LWJGL display, typically you would obtain the values
		      //from the properties dialog.
		      display = DisplaySystem.getDisplaySystem("LWJGL");
		 
		      //We'll create a 640x480x32 fullscreen display with a 60Hz monitor
		      //again these values will be typically obtained from the properties
		      display.createWindow(640,480,32,60,true);
		}
		catch (JmeException e) {
		      e.printStackTrace();
		      System.exit(1);
		}
		
		//Create renderer
		try {
			Renderer renderer = display.getRenderer();
		}
		catch (JmeException e) {
			e.printStackTrace();
			System.exit(1);			
		}
		
		//Get display/renderer system information
		//Save to log or file (TBD)
		String adapter = display.getAdapter();
		String openGLVers = display.getDisplayAPIVersion();
		String cardType = display.getDisplayRenderer();
		String cardVendor = display.getDisplayVendor();
		String cardDriver = display.getDriverVersion();

		
		//Create game view(s)
		
		//Initialize font system
		
		//Initialize input system?
		
		return true;
	}
	
	private void registerBaseGameEvents() {
		//TBD.  Register all starting game events
		//1) Check for eventManager existence
		//2) Register events with manager by calling eventManager.registerCodeOnlyEvent();
		
		
	}

	public int getCPUSpeed() {
		//TBD
		return 0;
	}

	public boolean isOnlyInstance()
	{
		//TBD:  http://www.rbgrn.net/content/43-java-single-application-instance
		
		return true;
	}
	
	void checkMemory(int physicalRAMNeed, int virtualRAMNeed) {
		//TBD
		
	}
	
	
	void checkHardDisk(int diskSpaceNeed) {
		//TBD
	}
	
	public static void main(String[] args) 
	{
		

	}

}
